package com.example.lab_lightbulb_v1;

import android.app.Activity;
import android.os.Bundle;
import android.view.Menu;
import android.view.View;
import android.widget.CompoundButton;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.SeekBar;
import android.widget.Switch;
import android.widget.ToggleButton;

public class MainActivity extends Activity {
	private ImageView imageView2;
	private SeekBar seekBar1;
	// private Switch switch1;
	private LinearLayout switch1;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		initView();
		initHandler();
	}

	private void initHandler() {
		// TODO Auto-generated method stub
		/*
		 * switch1.setOnClickListener(new View.OnClickListener() {
		 * 
		 * @Override public void onClick(View v) { boolean status = ((Switch)v
		 * ).isChecked(); initUI(status); } });
		 * 
		 * switch1.setOnCheckedChangeListener(new
		 * CompoundButton.OnCheckedChangeListener(){
		 * 
		 * @Override public void onCheckedChanged(CompoundButton arg0, boolean
		 * arg1) { // TODO Auto-generated method stub initUI(arg1); }
		 * 
		 * });
		 */

		seekBar1.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {

			@Override
			public void onStopTrackingTouch(SeekBar seekBar) {
				// TODO Auto-generated method stub

			}

			@Override
			public void onStartTrackingTouch(SeekBar seekBar) {
				// TODO Auto-generated method stub

			}

			@Override
			public void onProgressChanged(SeekBar seekBar, int progress,
					boolean fromUser) {
				// TODO Auto-generated method stub
				imageView2.setAlpha((float) (progress / 100.0));
			}
		});
	}

	private void initView() {
		// TODO Auto-generated method stub
		// 1. Find Attribute
		imageView2 = (ImageView) this.findViewById(R.id.imageView2);
		seekBar1 = (SeekBar) this.findViewById(R.id.seekBar1);
		switch1 = (LinearLayout) this.findViewById(R.id.switch1);

		// 2. UI Logic
		// Switch is off
		// seekBar1.setEnabled(false);//���i�H����
		// imageView2.setVisibility(View.INVISIBLE);
		initUI(false);

		int version = android.os.Build.VERSION.SDK_INT;
		if (version >= 14) {
			final Switch switcher = new Switch(this);
			switcher.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
				@Override
				public void onCheckedChanged(CompoundButton arg0, boolean isOn) {
					initUI(isOn);
				}
			});
			switcher.setChecked(false);
			switch1.addView(switcher);
		} else {
			final ToggleButton switcher = new ToggleButton(this);
			switcher.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {

				@Override
				public void onCheckedChanged(CompoundButton arg0, boolean isOn) {
					// TODO Auto-generated method stub
					initUI(isOn);
				}
			});
			switcher.setChecked(false);
			switch1.addView(switcher);
		}

	}

	private void initUI(boolean isOn) {
		/*
		 * if(isOn){ seekBar1.setEnabled(true);//���i�H����
		 * imageView2.setVisibility(View.VISIBLE); }else{
		 * seekBar1.setEnabled(false); imageView2.setVisibility(View.INVISIBLE);
		 * }
		 */
		seekBar1.setEnabled(isOn);
		imageView2.setVisibility(isOn ? View.VISIBLE : View.INVISIBLE);

	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.main, menu);
		return true;
	}

}
